//app.js
App({
  onLaunch: function () {
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        console.log('onCheckForUpdate====', res)
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          console.log('res.hasUpdate====')
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                console.log('success====', res)
                // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    }



    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    let version=wx.getSystemInfoSync().SDKVersion;
   // console.log(version)
    //console.log(this.compareVersion(version, '2.1.2 '))
    if (this.compareVersion(version,'2.1.2 ')>=0){
           //console.log(wx.getLaunchOptionsSync());
      let referPhone = wx.getLaunchOptionsSync().query.referPhone;
      console.log(referPhone)
      let scene = wx.getLaunchOptionsSync().scene;
      if (referPhone){
        wx.setStorageSync('referPhone', referPhone);//有推荐人
        console.log(referPhone)
      }
      let self = this;
      this.globalData.authorization = wx.getStorageSync('authorization');
      console.log(this.globalData.authorization)
      logs.unshift(Date.now())
      wx.setStorageSync('logs', logs)
      let userInfo = wx.getStorageSync('userInfo');
      let isLogin = wx.getStorageSync('isLogin');
    //  console.log('userInfo:' + JSON.stringify(userInfo))
      if (userInfo.phone) {
        // if (isLogin){
        wx.switchTab({
          url: '/pages/index/index',
        })
      }else{
        wx.redirectTo({
          url: '/pages/login/login',
        })
      } 
    }else{
      console.log("过低") 
      wx.reLaunch({
        url: '/pages/update/update',
      })
      // wx.showModal({
      //   title: '提示',
      //   content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
      //   success(res) {
      //     if (res.confirm) {
      //       wx.navigateBack({
      //         delta: 1
      //       })
           
      //     } else if (res.cancel) {
      //       wx.navigateBack({
      //         delta: 1
      //       })
           
      //     }
      //   }
      // })
      
    }
  },
  globalData: {
    userInfo: null,
    reqPath:'https://weapp.jimeiyouzhuang.com/',
    imgPath: 'https://source.pstech360.com/imgforgongbi/',
    authorization:''
  },
  compareVersion(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
      v1.push('0')
    }
    while (v2.length < len) {
      v2.push('0')
    }

    for (let i = 0; i < len; i++) {
      const num1 = parseInt(v1[i])
      const num2 = parseInt(v2[i])

      if (num1 > num2) {
        return 1
      } else if (num1 < num2) {
        return -1
      }
    }

    return 0
  }
})